Imports TAPI3Lib

<System.Runtime.InteropServices.ComVisible(False)> <CLSCompliant(False)> Public Class VBTAPI

   ' Constants
   Private Const MediaAudio As Integer = 8
   Private Const MediaModem As Integer = 16
   Private Const MediaFax As Integer = 32
   Private Const MediaVideo As Integer = 32768

   ' Public Events
   Delegate Sub fcnIncomingCall(ByVal CallNotification As ITCallNotificationEvent)
   Public Event IncomingCall As fcnIncomingCall

   ' Public Members
   Public WithEvents oTAPI As TAPI3Lib.TAPI ' will hold our TAPI object

   Private CallNotification As ITCallNotificationEvent
   Private CallState As ITCallStateEvent
   Private CallInfo As ITCallInfoChangeEvent

   Private oAddress As ITAddress ' will hold our selected address (you can hold many address in an array)
   Private oMediaTypes As Integer ' a variable to hold supported media types for the address
   Private RegCookie As Integer

   Sub New()

      ' Init TAPI
      Dim TAPI As New TAPIClass
      TAPI.Initialize()
      oTAPI = TAPI
      TAPI = Nothing

   End Sub

   Public Sub ChangeLine(ByVal LineName As String)
      UnRegister()
      SetAddress(LineName)
      RegisterCallNotifications()
   End Sub

   Private Sub SetAddress(ByVal LineName As String)
      ' Set the line address

      oAddress = Nothing

      ' Loop through address collection
      Dim AddressCollection As ITCollection = oTAPI.Addresses()
      For Each oAddress In AddressCollection
         If oAddress.AddressName = LineName Then
            ' Extract media support interface from the address
            Dim MediaSupport As ITMediaSupport = oAddress
            ' Extract supported media types
            oMediaTypes = MediaSupport.MediaTypes
            Exit For
         End If
      Next

   End Sub

   Private Sub RegisterCallNotifications()
      ' Register for notification of calls
      Try
         If Not oAddress Is Nothing Then
            ' registering notifications for the selected address
            RegCookie = oTAPI.RegisterCallNotifications(oAddress, True, False, oMediaTypes, 1)
            ' Note: this registration can be done on as many adresses as you want
            ' we will not receive notifications unless we spacify which type of events we are interested in
            oTAPI.EventFilter = (TAPI_EVENT.TE_CALLNOTIFICATION Or TAPI_EVENT.TE_CALLSTATE Or TAPI_EVENT.TE_CALLINFOCHANGE)
         Else
            Throw New Exception("Line address not specified")
         End If
      Catch ex As Exception
         Throw New Exception("Unable to register for call notifications", ex)
      End Try
   End Sub

   Private Sub oTAPI_Event(ByVal TapiEvent As TAPI3Lib.TAPI_EVENT, ByVal pEvent As Object) Handles oTAPI.Event

      ' making a thread to asynchronosly process the event
      Dim thAsyncCall As System.Threading.Thread

      Select Case TapiEvent
         Case TAPI_EVENT.TE_CALLNOTIFICATION 'Call Notification Arrived

            ' Threaded
            thAsyncCall = New Threading.Thread(AddressOf CallNotificationEvent)
            CallNotification = CType(pEvent, ITCallNotificationEvent)
            thAsyncCall.Start()

            ' Unthreaded
            'CallNotification = CType(pEvent, ITCallNotificationEvent)
            'CallNotificationEvent()

         Case TAPI_EVENT.TE_CALLSTATE 'Call State Changes

            ' assigning our sub's delegate to the thread
            thAsyncCall = New Threading.Thread(AddressOf CallStateEvent)
            'passing the variable for the thread
            CallState = CType(pEvent, ITCallStateEvent)
            ' starting the thread
            thAsyncCall.Start()

         Case TAPI_EVENT.TE_CALLINFOCHANGE 'Call Info Changes

            ' assigning our sub's delegate to the thread
            thAsyncCall = New Threading.Thread(AddressOf CallInfoEvent)
            'passing the variable for the thread
            CallInfo = CType(pEvent, ITCallInfoChangeEvent)
            ' starting the thread
            thAsyncCall.Start()

      End Select

   End Sub

   Private Sub CallNotificationEvent()
      ' here we should check to see various notifications of new and ended calls

      Select Case CallNotification.Event

         Case CALL_NOTIFICATION_EVENT.CNE_MONITOR
            ' the notification is for a monitored call

            RaiseEvent IncomingCall(CallNotification)


         Case CALL_NOTIFICATION_EVENT.CNE_OWNER
            ' the notification is for an owned call
      End Select

   End Sub

   Private Sub CallStateEvent()
      ' here we should check to see call state and handle connects and disconnects

      Select Case CallState.State
         Case CALL_STATE.CS_IDLE

         Case CALL_STATE.CS_INPROGRESS

         Case CALL_STATE.CS_OFFERING
            ' a call  is offering so if you dont want it then pass it 

            ' the code to pass the call is the following
            'Dim CallControl As ITBasicCallControl = CallState.Call
            'CallControl.HandoffIndirect (CallState.Call.CallInfoLong(CALLINFO_LONG.CIL_MEDIATYPESAVAILABLE)

         Case CALL_STATE.CS_CONNECTED
            ' call  is connected 

         Case CALL_STATE.CS_QUEUED
            ' call is beeing queued

         Case CALL_STATE.CS_HOLD
            ' call is on hold 

         Case CALL_STATE.CS_DISCONNECTED
            ' call is disconnected

      End Select

   End Sub

   Private Sub CallInfoEvent()
      ' here you can extract information from the call 

      'the code to extract the caller ID
      Dim CallerID As String
      CallerID = CallInfo.Call.CallInfoString(CALLINFO_STRING.CIS_CALLERIDNAME)

   End Sub

   Private Sub UnRegister()
      If RegCookie > 0 Then oTAPI.UnregisterNotifications(RegCookie)
   End Sub

   Public Sub Dispose()
      ' Dispose of TAPI Object
      UnRegister()
      oTAPI.Shutdown()
   End Sub

End Class
